package com.itheima.service;

import com.itheima.cloud.pojo.Order;
import com.itheima.cloud.response.OrderVO;
import com.itheima.mapper.OrderMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    Mapper mapper;

    public List<OrderVO> findOrderByUsername(String username) {

        //参数校验
        if (username == null){
            throw new RuntimeException("参数非法");
        }

        Example example = new Example(Order.class);
        //条件查询 mybatis 动态查询 动态SQL where username=''
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",username);

        List<Order> orderList = orderMapper.selectByExample(example);

        if (orderList == null ){
            return new ArrayList<>();
        }

        List<OrderVO> collect = orderList.stream().map(order -> {
            return mapper.map(order, OrderVO.class);
        } ).collect(Collectors.toList());

        System.out.println(collect);

        return collect;

    }
}
